package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import util.Utilities;

import dao.ChapterSession;
import dao.Examination;

public class HandinExaminationServlet extends HttpServlet {

    /**
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
	    throws ServletException, IOException {

	this.doPost(request, response);
    }

    /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
	    throws ServletException, IOException {
	
	HttpSession session = request.getSession();
	String countString1 = request.getParameter("aCounts");
	String countString2 = request.getParameter("bCounts");
	String countString3 = request.getParameter("cCounts");
	String typeString = request.getParameter("type").trim();
	int counts1 = Integer.parseInt(countString1);
	int counts2 = Integer.parseInt(countString2);
	int counts3 = Integer.parseInt(countString3);
	List<Examination> item1 = (List<Examination>)session.getAttribute("item1");
	List<Examination> item2 = (List<Examination>)session.getAttribute("item2");
	List<Examination> item3 = (List<Examination>)session.getAttribute("item3");
	String chapteridString = item1.get(0).getChapterid();
	List<ChapterSession> sessionList = (List<ChapterSession>)session.getAttribute("chapterSession");
	
	int[] isRight = (int[])session.getAttribute("isRight1");
	//List<Integer> ids1 = (List<Integer>)session.getAttribute("item1Ids");
	session.removeAttribute("item1");
	session.removeAttribute("item2");
	session.removeAttribute("item3");
	session.removeAttribute("isRight1");
	session.removeAttribute("item1Ids");
	List<Integer> errorList = new ArrayList<Integer>();
	List<Integer> examinationIds = new ArrayList<Integer>();
	for(int i=0;i<isRight.length;i++){	    
	    if(isRight[i] != 1){
		errorList.add(item1.get(i).getKnowledgeid());
	    }else{
		examinationIds.add(item1.get(i).getExaminationid());
	    }
	}
//	if(counts1 > 0){
//	    for(int i=0;i<counts1;i++){
//		examinationIds.add(item1.get(i).getExaminationid());
//	    }
//	}
	if(counts2 > 0){
	    for(int i=0;i<counts2;i++){		
		String answer = request.getParameter("bIsRight"+(i+1));
		if("0".equals(answer)){
		    errorList.add(item2.get(i).getKnowledgeid());
		}else{
		    examinationIds.add(item2.get(i).getExaminationid());  
		}
	    }
	}
	if(counts3 > 0){
	    for(int i=0;i<counts3;i++){		
		String answer = request.getParameter("cIsRight"+(i+1));
		if("0".equals(answer)){
		    errorList.add(item3.get(i).getKnowledgeid());
		}else{
		    examinationIds.add(item3.get(i).getExaminationid());
		}
	    }
	}	
	Utilities.removeDuplicateWithOrder(errorList);
	Utilities.removeDuplicateWithOrder(examinationIds);
	//去除重复，error,examinations
	
	if(sessionList == null){
	    sessionList = new ArrayList<ChapterSession>();
	    ChapterSession chapterSession = new ChapterSession();
	    chapterSession.setChapterid(chapteridString);
	    chapterSession.setErrorSessionName("errorKnowledges1");
	    chapterSession.setIdSessionName("doneItems1");
	    sessionList.add(chapterSession);
	    session.setAttribute("chapterSession", sessionList);
	    session.setAttribute("errorKnowledges1", errorList);
	    session.setAttribute("doneItems1", examinationIds);
	}else{
	    boolean notFound = true;
	    for(int j=0;j<sessionList.size();j++){
		if(chapteridString.equals(sessionList.get(j).getChapterid())){			    
		    session.setAttribute(sessionList.get(j).getErrorSessionName(), errorList);
		    examinationIds.addAll((List<Integer>)session.getAttribute((sessionList.get(j).getIdSessionName())));
		    session.setAttribute(sessionList.get(j).getIdSessionName(), examinationIds);
		    notFound = false;
		    break;
		}
	    }
	    if(notFound){
		ChapterSession chapterSession = new ChapterSession();
		    chapterSession.setChapterid(chapteridString);
		    int counts = sessionList.size();
		    counts++;
		    String errorName = "errorKnowledges"+counts;
		    String idName = "doneItems" + counts;		    
		    chapterSession.setErrorSessionName(errorName);
		    chapterSession.setIdSessionName(idName);
		    sessionList.add(chapterSession);
		    session.setAttribute("chapterSession", sessionList);
		    session.setAttribute(errorName, errorList);
		    session.setAttribute(idName, examinationIds);
	    }
	}
	if("go".equals(typeString)){
	    response.sendRedirect("../common/examination.jsp?chapterid="+chapteridString);
	}else if("back".equals(typeString)){
	    response.sendRedirect("../common/chapter.jsp?chapterid="+chapteridString);
	}
//	PrintWriter out =  response.getWriter();
//	out.write(item1.size());
//	out.write(errorList.size()+"---"+examinationIds.size());
//	for(int i:errorList){
//	    out.write(i);
//	    out.write("&nbsp;&nbsp;");
//	}
//	out.write("<hr>");
//	for(int i:examinationIds){
//	    out.write(i);
//	    out.write("&nbsp;&nbsp;");
//	}
    }

}
